﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibKetoan;

namespace Ketoan
{
	/// <summary>
	/// Summary description for frmBkchungtu.
	/// </summary>
	public class frmBkchungtu : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.Button butLuu;
		private System.Windows.Forms.Button butKetthuc;
		private System.Windows.Forms.Button butTonghop;
		private int i_userd;
		private string s_mmyy,sql,user;
		private AccessData a;
		private DataSet ds;
		private Brush disabledBackBrush;
		private Brush disabledTextBrush;
		private Brush alertBackBrush;
		private Font alertFont;
		private Brush alertTextBrush;
		private Font currentRowFont;
		private Brush currentRowBackBrush;
		private bool afterCurrentCellChanged;
		private int checkCol=0;
		private System.Windows.Forms.CheckBox chkAll;
		private System.Windows.Forms.ToolTip toolTip1;
		private System.Windows.Forms.NumericUpDown qui;
		private System.Windows.Forms.NumericUpDown tu;
		private System.Windows.Forms.NumericUpDown den;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox sotk;
		private System.Windows.Forms.TextBox mtm;
		private System.Windows.Forms.Button butIn;
		private System.ComponentModel.IContainer components;
		private string stk="661",sNghiepvu="2,3,4,5,6";

		public frmBkchungtu(AccessData acc,string mmyy,int userid)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			a=acc;s_mmyy=mmyy;i_userd=userid;
			this.Text+=" năm 20"+mmyy.Substring(2);
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
					if(disabledBackBrush != null)
					{
						disabledBackBrush.Dispose();
						disabledTextBrush.Dispose();
						alertBackBrush.Dispose();
						alertFont.Dispose();
						alertTextBrush.Dispose();
						currentRowFont.Dispose();
						currentRowBackBrush.Dispose();
					}
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmBkchungtu));
			this.label5 = new System.Windows.Forms.Label();
			this.sotk = new System.Windows.Forms.TextBox();
			this.mtm = new System.Windows.Forms.TextBox();
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.butLuu = new System.Windows.Forms.Button();
			this.butKetthuc = new System.Windows.Forms.Button();
			this.butTonghop = new System.Windows.Forms.Button();
			this.chkAll = new System.Windows.Forms.CheckBox();
			this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
			this.qui = new System.Windows.Forms.NumericUpDown();
			this.tu = new System.Windows.Forms.NumericUpDown();
			this.den = new System.Windows.Forms.NumericUpDown();
			this.label2 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.butIn = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.qui)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.tu)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.den)).BeginInit();
			this.SuspendLayout();
			// 
			// label5
			// 
			this.label5.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
			this.label5.Location = new System.Drawing.Point(464, 5);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(32, 23);
			this.label5.TabIndex = 4;
			this.label5.Text = "Mục :";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// sotk
			// 
			this.sotk.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.sotk.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.sotk.Location = new System.Drawing.Point(328, 5);
			this.sotk.Name = "sotk";
			this.sotk.Size = new System.Drawing.Size(136, 21);
			this.sotk.TabIndex = 6;
			this.sotk.Text = "";
			this.sotk.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
			this.sotk.TextChanged += new System.EventHandler(this.sotk_TextChanged);
			// 
			// mtm
			// 
			this.mtm.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
			this.mtm.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.mtm.Location = new System.Drawing.Point(496, 5);
			this.mtm.Name = "mtm";
			this.mtm.Size = new System.Drawing.Size(104, 21);
			this.mtm.TabIndex = 7;
			this.mtm.Text = "";
			this.mtm.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
			this.mtm.TextChanged += new System.EventHandler(this.mtm_TextChanged);
			// 
			// dataGrid1
			// 
			this.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Control;
			this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Control;
			this.dataGrid1.DataMember = "";
			this.dataGrid1.FlatMode = true;
			this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(8, 12);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.RowHeaderWidth = 5;
			this.dataGrid1.Size = new System.Drawing.Size(776, 492);
			this.dataGrid1.TabIndex = 24;
			this.dataGrid1.Click += new System.EventHandler(this.dataGrid1_Click);
			this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
			// 
			// butLuu
			// 
			this.butLuu.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butLuu.Image = ((System.Drawing.Bitmap)(resources.GetObject("butLuu.Image")));
			this.butLuu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butLuu.Location = new System.Drawing.Point(332, 512);
			this.butLuu.Name = "butLuu";
			this.butLuu.Size = new System.Drawing.Size(74, 25);
			this.butLuu.TabIndex = 3;
			this.butLuu.Text = "&Cập nhật";
			this.butLuu.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butLuu.Click += new System.EventHandler(this.butLuu_Click);
			// 
			// butKetthuc
			// 
			this.butKetthuc.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butKetthuc.Image = ((System.Drawing.Bitmap)(resources.GetObject("butKetthuc.Image")));
			this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butKetthuc.Location = new System.Drawing.Point(466, 512);
			this.butKetthuc.Name = "butKetthuc";
			this.butKetthuc.Size = new System.Drawing.Size(70, 25);
			this.butKetthuc.TabIndex = 5;
			this.butKetthuc.Text = "   &Kết thúc";
			this.butKetthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
			// 
			// butTonghop
			// 
			this.butTonghop.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butTonghop.Image = ((System.Drawing.Bitmap)(resources.GetObject("butTonghop.Image")));
			this.butTonghop.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butTonghop.Location = new System.Drawing.Point(256, 512);
			this.butTonghop.Name = "butTonghop";
			this.butTonghop.Size = new System.Drawing.Size(74, 25);
			this.butTonghop.TabIndex = 2;
			this.butTonghop.Text = "&Tổng hợp";
			this.butTonghop.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butTonghop.Click += new System.EventHandler(this.butTonghop_Click);
			// 
			// chkAll
			// 
			this.chkAll.Appearance = System.Windows.Forms.Appearance.Button;
			this.chkAll.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.chkAll.Location = new System.Drawing.Point(25, 32);
			this.chkAll.Name = "chkAll";
			this.chkAll.Size = new System.Drawing.Size(23, 21);
			this.chkAll.TabIndex = 29;
			this.chkAll.Text = "...";
			this.chkAll.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			this.toolTip1.SetToolTip(this.chkAll, "Chọn cả");
			this.chkAll.CheckedChanged += new System.EventHandler(this.chkAll_CheckedChanged);
			// 
			// qui
			// 
			this.qui.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
			this.qui.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.qui.Location = new System.Drawing.Point(752, 5);
			this.qui.Maximum = new System.Decimal(new int[] {
																4,
																0,
																0,
																0});
			this.qui.Minimum = new System.Decimal(new int[] {
																1,
																0,
																0,
																0});
			this.qui.Name = "qui";
			this.qui.Size = new System.Drawing.Size(32, 21);
			this.qui.TabIndex = 8;
			this.qui.Value = new System.Decimal(new int[] {
															  1,
															  0,
															  0,
															  0});
			this.qui.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
			// 
			// tu
			// 
			this.tu.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.tu.Location = new System.Drawing.Point(144, 5);
			this.tu.Maximum = new System.Decimal(new int[] {
															   12,
															   0,
															   0,
															   0});
			this.tu.Minimum = new System.Decimal(new int[] {
															   1,
															   0,
															   0,
															   0});
			this.tu.Name = "tu";
			this.tu.Size = new System.Drawing.Size(40, 21);
			this.tu.TabIndex = 0;
			this.tu.Value = new System.Decimal(new int[] {
															 1,
															 0,
															 0,
															 0});
			this.tu.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
			// 
			// den
			// 
			this.den.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.den.Location = new System.Drawing.Point(216, 5);
			this.den.Maximum = new System.Decimal(new int[] {
																12,
																0,
																0,
																0});
			this.den.Minimum = new System.Decimal(new int[] {
																1,
																0,
																0,
																0});
			this.den.Name = "den";
			this.den.Size = new System.Drawing.Size(40, 21);
			this.den.TabIndex = 1;
			this.den.Value = new System.Decimal(new int[] {
															  1,
															  0,
															  0,
															  0});
			this.den.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tu_KeyDown);
			// 
			// label2
			// 
			this.label2.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
			this.label2.Location = new System.Drawing.Point(592, 5);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(160, 23);
			this.label2.TabIndex = 34;
			this.label2.Text = "Đề nghị thanh toán trong quí :";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(-8, 5);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(152, 23);
			this.label7.TabIndex = 35;
			this.label7.Text = "Chứng từ phát sinh từ tháng :";
			this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(184, 5);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(32, 23);
			this.label8.TabIndex = 36;
			this.label8.Text = "Đến :";
			this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(248, 5);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(80, 23);
			this.label4.TabIndex = 3;
			this.label4.Text = "Số tài khoản :";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// butIn
			// 
			this.butIn.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.butIn.Image = ((System.Drawing.Bitmap)(resources.GetObject("butIn.Image")));
			this.butIn.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butIn.Location = new System.Drawing.Point(408, 512);
			this.butIn.Name = "butIn";
			this.butIn.Size = new System.Drawing.Size(56, 25);
			this.butIn.TabIndex = 4;
			this.butIn.Text = "     &In";
			// 
			// frmBkchungtu
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(792, 573);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.butIn,
																		  this.mtm,
																		  this.label8,
																		  this.label7,
																		  this.label2,
																		  this.den,
																		  this.tu,
																		  this.qui,
																		  this.chkAll,
																		  this.butTonghop,
																		  this.butKetthuc,
																		  this.butLuu,
																		  this.sotk,
																		  this.label5,
																		  this.label4,
																		  this.dataGrid1});
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.KeyPreview = true;
			this.Name = "frmBkchungtu";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Bảng kê chứng từ đề nghị thanh toán tạm ứng kinh phí";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Load += new System.EventHandler(this.frmBkchungtu_Load);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.qui)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.tu)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.den)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void frmBkchungtu_Load(object sender, System.EventArgs e)
		{			
			user=a.user;
			int mm=int.Parse(s_mmyy.Substring(0,2));
			if (mm<4) qui.Value=1;
			else if (mm<7) qui.Value=2;
			else if (mm<10) qui.Value=3;
			else qui.Value=4;

			tu.Value=(mm<4)?1:(mm<7)?3:(mm<10)?6:10;
			den.Value=decimal.Parse(s_mmyy.Substring(0,2));

			load_grid();
			AddGridTableStyle();
			
			this.disabledBackBrush = new SolidBrush(Color.FromArgb(255,255,192));
			this.disabledTextBrush = new SolidBrush(Color.FromArgb(255,0,0));

			this.alertBackBrush = new SolidBrush(SystemColors.HotTrack);
			this.alertFont = new Font(this.dataGrid1.Font.Name, this.dataGrid1.Font.Size, FontStyle.Bold);
			this.alertTextBrush = new SolidBrush(Color.White);
			
			this.currentRowFont = new Font(this.dataGrid1.Font.Name, this.dataGrid1.Font.Size, FontStyle.Regular);
			this.currentRowBackBrush = new SolidBrush(Color.FromArgb(0,255, 255));		
		}	

		private void load_grid()
		{
			ds=new DataSet();
			string mm=(qui.Value==1)?"03":(qui.Value==2)?"06":(qui.Value==3)?"09":"12";
			ds.ReadXml("..\\..\\..\\xml\\d_thanhtoan.xml");
			sql="select b.id,a.soctg,to_char(a.ngayg,'dd/mm/yyyy') as ngayg,to_char(a.ngaygs,'dd/mm/yyyy') as ngaygs,b.noidung,";
			sql+="c.stk as tkno,nvl(d.stk,' ') as mtmno,e.stk as tkco,nvl(f.stk,' ') as mtmco,b.stien as sotien,nvl(b.thangqt,' ') as thangqt";
			sql+=" from a_ct"+s_mmyy.Substring(2)+" a inner join a_ctct"+s_mmyy.Substring(2)+" b on a.id=b.idct ";
			sql+=" inner join a_dmtk c on b.tknoa=c.id left join a_dmch d on b.mtmno=d.id ";
			sql+=" inner join a_dmtk e on b.tkcoa=e.id left join a_dmch f on b.mtmco=f.id ";
			sql+=" where a.thanggs between '"+tu.Value.ToString().PadLeft(2,'0')+"' and '"+den.Value.ToString().PadLeft(2,'0')+"'";
			sql+=" and a.thanggs<='"+mm+"'";
			sql+=" and (b.thangqt='"+mm+"' or b.thangqt is null)";
			sql+=" and substr(c.stk,1,3)='"+stk+"' and a.nghiepvu in ("+sNghiepvu+")";
			sql+=" order by a.soctg";
			ds=a.get_data(sql);				
			ds.Tables[0].Columns.Add("Chon",typeof(bool));
			foreach(DataRow r in ds.Tables[0].Rows) r["chon"]=(r["thangqt"].ToString().Trim()!="")?true:false;
			dataGrid1.DataSource=ds.Tables[0];
			CurrencyManager cm = (CurrencyManager)BindingContext[dataGrid1.DataSource,dataGrid1.DataMember]; 
			DataView dv = (DataView) cm.List; 
			dv.AllowNew = false; 
		}

		private void AddGridTableStyle()
		{
			DataGridTableStyle ts =new DataGridTableStyle();
			ts.MappingName = ds.Tables[0].TableName;
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.FromArgb(0,255,255);
			ts.SelectionForeColor = Color.PaleGreen;
			ts.RowHeaderWidth=5;
					
			FormattableBooleanColumn discontinuedCol = new FormattableBooleanColumn();
			discontinuedCol.MappingName = "chon";
			discontinuedCol.HeaderText = "Chọn";
			discontinuedCol.Width = 30;
			discontinuedCol.AllowNull = false;

			discontinuedCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			discontinuedCol.BoolValueChanged += new BoolValueChangedEventHandler(BoolValueChanged);
			ts.GridColumnStyles.Add(discontinuedCol);
			dataGrid1.TableStyles.Add(ts);

			FormattableTextBoxColumn TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "id";
			TextCol.HeaderText = "";
			TextCol.Width =0;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "soctg";
			TextCol.HeaderText = "Số CT";
			TextCol.Width =80;
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol = new FormattableTextBoxColumn();
			TextCol.MappingName = "ngayg";
			TextCol.HeaderText = "Ngày";
			TextCol.Width =65;
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new FormattableTextBoxColumn();
			TextCol.MappingName = "noidung";
			TextCol.HeaderText = "Diễn giải";
			TextCol.Width = dataGrid1.Width-(500+65+80+30+20);
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new FormattableTextBoxColumn();
			TextCol.MappingName = "tkno";
			TextCol.HeaderText = "TK Nợ";
			TextCol.Width = 100;
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new FormattableTextBoxColumn();
			TextCol.MappingName = "mtmno";
			TextCol.HeaderText = "M/TM Nợ";
			TextCol.Width = 100;
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new FormattableTextBoxColumn();
			TextCol.MappingName = "tkco";
			TextCol.HeaderText = "TK Có";
			TextCol.Width =100;
			TextCol.ReadOnly=true;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new FormattableTextBoxColumn();
			TextCol.MappingName = "mtmco";
			TextCol.HeaderText = "M/TM Có";
			TextCol.Width =100;
			TextCol.ReadOnly=false;
			TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new FormattableTextBoxColumn();
			TextCol.MappingName = "sotien";
			TextCol.HeaderText = "Số tiền";
			TextCol.Format="### ### ### ##0.00";
			TextCol.Width =100;
			TextCol.ReadOnly=true;
			TextCol.Alignment=HorizontalAlignment.Right;
			//TextCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new FormattableTextBoxColumn();
			TextCol.MappingName = "thangqt";
			TextCol.HeaderText = "";
			TextCol.Width =0;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);
		}
	
		private void SetCellFormat(object sender, DataGridFormatCellEventArgs e)
		{
			try
			{
				bool discontinued = (bool) ((e.Column != 0) ? this.dataGrid1[e.Row, 0] : e.CurrentCellValue);
				if(e.Column > 0 && (bool)(this.dataGrid1[e.Row, 0]))//discontinued)
				{
					e.ForeBrush = this.disabledTextBrush;
				}
				else if(e.Column > 0 && e.Row == this.dataGrid1.CurrentRowIndex)
				{
					e.BackBrush = this.currentRowBackBrush;
					e.TextFont = this.currentRowFont;
				}
				if (dataGrid1[e.Row,10].ToString().Trim()!="") e.ForeBrush=new SolidBrush(Color.Orange);
			}
			catch{}
		}

		private void BoolValueChanged(object sender, BoolValueChangedEventArgs e)
		{
			this.dataGrid1.EndEdit(this.dataGrid1.TableStyles[0].GridColumnStyles["Discontinued"],e.Row, false);
			RefreshRow(e.Row);
			this.dataGrid1.BeginEdit(this.dataGrid1.TableStyles[0].GridColumnStyles["Discontinued"],e.Row);
		}
		private void RefreshRow(int row)
		{
			Rectangle rect = this.dataGrid1.GetCellBounds(row, 0);
			rect = new Rectangle(rect.Right, rect.Top, this.dataGrid1.Width, rect.Height);
			this.dataGrid1.Invalidate(rect);
		}

		private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
		{
			if((bool)this.dataGrid1[this.dataGrid1.CurrentRowIndex, checkCol])
				this.dataGrid1.CurrentCell = new DataGridCell(this.dataGrid1.CurrentRowIndex, checkCol);
			afterCurrentCellChanged = true;
		}

		private void dataGrid1_Click(object sender, System.EventArgs e)
		{
			Point pt = this.dataGrid1.PointToClient(Control.MousePosition);
			DataGrid.HitTestInfo hti = this.dataGrid1.HitTest(pt);
			BindingManagerBase bmb = this.BindingContext[this.dataGrid1.DataSource, this.dataGrid1.DataMember];
			if(afterCurrentCellChanged && hti.Row < bmb.Count 
				&& hti.Type == DataGrid.HitTestType.Cell 
				&&  hti.Column == checkCol )
			{	
				this.dataGrid1[hti.Row, checkCol] = !(bool)this.dataGrid1[hti.Row, checkCol];
				RefreshRow(hti.Row);
			}
			afterCurrentCellChanged = false;
		}

		private void tu_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");
		}

		private void ref_grid()
		{
			CurrencyManager cm= (CurrencyManager)BindingContext[dataGrid1.DataSource];
			DataView dv=(DataView)cm.List;
			sql="";
			if (sotk.Text!="")
			{
				if (sql!="") sql+=" and ";
				sql+=" (tkno like '%"+sotk.Text+"%' or tkco like '%"+sotk+"%')";
			}
			if (mtm.Text!="")
			{
				if (sql!="") sql+=" and ";
				sql+=" (mtmno like '%"+mtm.Text+"%' or mtmco like '%"+mtm+"%')";
			}
			dv.RowFilter=sql;
		}

		private void butTonghop_Click(object sender, System.EventArgs e)
		{
			load_grid();
		}

		private bool kiemtra()
		{
			string mm=(qui.Value==1)?"03":(qui.Value==2)?"06":(qui.Value==3)?"09":"12";
			if (a.bKhoaso(mm+s_mmyy.Substring(2)))
			{
				MessageBox.Show("Số liệu quí "+qui.Value.ToString()+" năm 20"+s_mmyy.Substring(2)+" đã khóa sổ !",a.Msg);
				tu.Focus();
				return false;
			}
			return true;
		}

		private void butLuu_Click(object sender, System.EventArgs e)
		{
			CurrencyManager cm= (CurrencyManager)BindingContext[dataGrid1.DataSource];
			DataView dv=(DataView)cm.List;
			dv.RowFilter="";
			if (!kiemtra()) return;
			string mm=(qui.Value==1)?"03":(qui.Value==2)?"06":(qui.Value==3)?"09":"12";
			bool bUpdate=false;
			foreach(DataRow r in ds.Tables[0].Rows)
			{
				sql = "select a.idbc,a.chuong,a.hopdong,a.madt,a.nghiepvu,a.hoatdong,b.lk,b.tknoa,b.tkcoa,b.kmno,b.kmco,b.kmno2,b.kmco2,b.kmno3,b.kmco3,b.kmno4,b.kmco4,b.mtmno,";
				sql +=" b.mtmco,b.stien,b.thangqt";
				sql += " from "+user+".a_ct" + s_mmyy.Substring(2) + " a,"+user+".a_ctct" + s_mmyy.Substring(2)+ " b where a.id=b.idct ";
				sql += " and b.id="+long.Parse(r["id"].ToString());
				if (r["chon"].ToString().Trim().ToLower()=="true" && r["thangqt"].ToString()!="")
				{
					foreach(DataRow r2 in a.get_data(sql).Tables[0].Rows)
					{
						a.updSodu("a_sdc",a.insert,mm+s_mmyy.Substring(2),long.Parse(r2["idbc"].ToString()),
							long.Parse(r2["chuong"].ToString()),long.Parse(r2["hopdong"].ToString()),
							long.Parse(r2["lk"].ToString()),long.Parse(r2["nghiepvu"].ToString()),
							long.Parse(r2["madt"].ToString()),long.Parse(r2["tknoa"].ToString()),long.Parse(r2["tkcoa"].ToString()),
							long.Parse(r2["kmno"].ToString()),long.Parse(r2["kmco"].ToString()),
							long.Parse(r2["kmno2"].ToString()),long.Parse(r2["kmco2"].ToString()),
							long.Parse(r2["kmno3"].ToString()),long.Parse(r2["kmco3"].ToString()),
							long.Parse(r2["kmno4"].ToString()),long.Parse(r2["kmco4"].ToString()),
							long.Parse(r2["mtmno"].ToString()),long.Parse(r2["mtmco"].ToString()),long.Parse(r2["hoatdong"].ToString()),
							decimal.Parse(r2["stien"].ToString()));
					}
					a.execute_data("update "+user+".a_ctct"+s_mmyy.Substring(2)+" set thangqt='"+mm+"' where id="+long.Parse(r["id"].ToString()));
				}
				else if (r["chon"].ToString().Trim().ToLower()=="false" && r["thangqt"].ToString().Trim()!="")
				{
					foreach(DataRow r2 in a.get_data(sql).Tables[0].Rows)
					{
						a.updSodu("a_sdc",a.delete,mm+s_mmyy.Substring(2),long.Parse(r2["idbc"].ToString()),
							long.Parse(r2["chuong"].ToString()),long.Parse(r2["hopdong"].ToString()),
							long.Parse(r2["lk"].ToString()),long.Parse(r2["nghiepvu"].ToString()),
							long.Parse(r2["madt"].ToString()),long.Parse(r2["tknoa"].ToString()),long.Parse(r2["tkcoa"].ToString()),
							long.Parse(r2["kmno"].ToString()),long.Parse(r2["kmco"].ToString()),
							long.Parse(r2["kmno2"].ToString()),long.Parse(r2["kmco2"].ToString()),
							long.Parse(r2["kmno3"].ToString()),long.Parse(r2["kmco3"].ToString()),
							long.Parse(r2["kmno4"].ToString()),long.Parse(r2["kmco4"].ToString()),
							long.Parse(r2["mtmno"].ToString()),long.Parse(r2["mtmco"].ToString()),long.Parse(r2["hoatdong"].ToString()),
							decimal.Parse(r2["stien"].ToString()));
					}
					a.execute_data("update "+user+".a_ctct"+s_mmyy.Substring(2)+" set thangqt=null where id="+long.Parse(r["id"].ToString()));
				}
				bUpdate=true;
			}
			if (bUpdate) a.updDtsd("",s_mmyy,i_userd);
			load_grid();
			butTonghop.Focus();
		}

		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			a.close();System.GC.Collect();this.Close();
		}

		private void chkAll_CheckedChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==chkAll)
				foreach(DataRow r in ds.Tables[0].Select("thangqt=' '")) r["chon"]=(chkAll.Checked)?true:false;
		}

		private void sotk_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==sotk) ref_grid();
		}

		private void mtm_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==mtm) ref_grid();
		}
	}
}		